package com.ptswitch.material.components;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.corelib.base.AbstractConditional;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.util.security.PermissionUtil;

public class IfPermitted extends AbstractConditional {
    @Parameter(defaultPrefix = BindingConstants.LITERAL)
    private String permission;

    @Inject
    private ComponentResources componentResources;

    @Override
    protected boolean test() {
        if (permission == null) {
            return true;
        }
        PageBase page = (PageBase) componentResources.getPage();
        String permission_ = permission;
        if (!permission.startsWith(PermissionUtil.All.getPermissionName())) {
            permission_ = PermissionUtil.All.getPermissionName() + "." + permission;
        }
        return page.checkPermitted(permission_, false);
    }

}
